package com.life.view;

import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JProgressBar;
import javax.swing.JRadioButton;
import javax.swing.JSlider;
import javax.swing.JSpinner;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.JToggleButton;
import javax.swing.UIManager;

import com.life.constants.Constants;
import com.life.controller.BoardSizeController;
import com.life.controller.GenerationController;
import com.life.controller.LoggerController;
import com.life.controller.RuleController;
import com.life.controller.SimulationController;
import com.life.controller.StatisticsController;
import com.life.controller.StatusController;
import com.life.model.LifeModel;
import com.life.rule.ILifeRule;
import com.life.rule.SBLifeRule;

public class InitModel
{

    public InitModel()
    {

	LifeModel model = new LifeModel(Constants.START_BOARD_SIZE,
		Constants.START_BOARD_SIZE);

	try
	{
	    UIManager
		    .setLookAndFeel(new com.sun.java.swing.plaf.windows.WindowsLookAndFeel());

	} catch (Exception ex)
	{
	    System.out.println("Could not set Windows Look and Feel");
	}

	// for(int i = 0; i < 1000; i++)
	// {
	// int y = (int)(Math.random() * model.getSize().getHeight());
	// int x = (int)(Math.random() * model.getSize().getWidth());
	//			
	// model.setAlive(x,y);
	// }
	LifeAppfrm appfrm = new LifeAppfrm(900, 600, model);
	appfrm.setVisible(true);

	/* create Adapters */
	WidgetRegistry widgetRegistry = WidgetRegistry.getInstance();
	ButtonAdapter playAdapter = new ButtonAdapter((JButton) widgetRegistry
		.get(Constants.PLAY_WIDGET));
	ButtonAdapter pauseAdapter = new ButtonAdapter((JButton) widgetRegistry
		.get(Constants.PAUSE_WIDGET));
	ButtonAdapter stopAdapter = new ButtonAdapter((JButton) widgetRegistry
		.get(Constants.STOP_WIDGET));
	ButtonAdapter recordAdapter = new ButtonAdapter(
		(JButton) widgetRegistry.get(Constants.RECORD_WIDGET));
	SliderAdapter sliderAdapter = new SliderAdapter(
		(JSlider) widgetRegistry.get(Constants.SLIDER_WIDGET));
	SpinnerAdapter spinnerAdapter = new SpinnerAdapter(
		(JSpinner) widgetRegistry.get(Constants.SPEED_WIDGET));

	RadioGroupAdapter groupAdapter = new RadioGroupAdapter(
		(ButtonGroup) widgetRegistry.get(Constants.RADIO_GROUP_WIDGET));

	TextFieldAdapter userBAdapter = new TextFieldAdapter(
		(JTextField) widgetRegistry
			.get(Constants.USER_DEFINED_BIRTH_WIDGET));
	TextFieldAdapter userSAdapter = new TextFieldAdapter(
		(JTextField) widgetRegistry
			.get(Constants.USER_DEFINED_SURVIVAL_WIDGET));

	AbstractAdapter s0Adapter = new TextFieldAdapter(
		(JTextField) widgetRegistry
			.get(Constants.PRBL_SURVIVAL_0_PERCENT_WIDGET));
	AbstractAdapter s1Adapter = new TextFieldAdapter(
		(JTextField) widgetRegistry
			.get(Constants.PRBL_SURVIVAL_1_PERCENT_WIDGET));
	AbstractAdapter s2Adapter = new TextFieldAdapter(
		(JTextField) widgetRegistry
			.get(Constants.PRBL_SURVIVAL_2_PERCENT_WIDGET));
	AbstractAdapter s3Adapter = new TextFieldAdapter(
		(JTextField) widgetRegistry
			.get(Constants.PRBL_SURVIVAL_3_PERCENT_WIDGET));
	AbstractAdapter s4Adapter = new TextFieldAdapter(
		(JTextField) widgetRegistry
			.get(Constants.PRBL_SURVIVAL_4_PERCENT_WIDGET));
	AbstractAdapter s5Adapter = new TextFieldAdapter(
		(JTextField) widgetRegistry
			.get(Constants.PRBL_SURVIVAL_5_PERCENT_WIDGET));
	AbstractAdapter s6Adapter = new TextFieldAdapter(
		(JTextField) widgetRegistry
			.get(Constants.PRBL_SURVIVAL_6_PERCENT_WIDGET));
	AbstractAdapter s7Adapter = new TextFieldAdapter(
		(JTextField) widgetRegistry
			.get(Constants.PRBL_SURVIVAL_7_PERCENT_WIDGET));
	AbstractAdapter s8Adapter = new TextFieldAdapter(
		(JTextField) widgetRegistry
			.get(Constants.PRBL_SURVIVAL_8_PERCENT_WIDGET));

	AbstractAdapter b0Adapter = new TextFieldAdapter(
		(JTextField) widgetRegistry
			.get(Constants.PRBL_BIRTH_0_PERCENT_WIDGET));
	AbstractAdapter b1Adapter = new TextFieldAdapter(
		(JTextField) widgetRegistry
			.get(Constants.PRBL_BIRTH_1_PERCENT_WIDGET));
	AbstractAdapter b2Adapter = new TextFieldAdapter(
		(JTextField) widgetRegistry
			.get(Constants.PRBL_BIRTH_2_PERCENT_WIDGET));
	AbstractAdapter b3Adapter = new TextFieldAdapter(
		(JTextField) widgetRegistry
			.get(Constants.PRBL_BIRTH_3_PERCENT_WIDGET));
	AbstractAdapter b4Adapter = new TextFieldAdapter(
		(JTextField) widgetRegistry
			.get(Constants.PRBL_BIRTH_4_PERCENT_WIDGET));
	AbstractAdapter b5Adapter = new TextFieldAdapter(
		(JTextField) widgetRegistry
			.get(Constants.PRBL_BIRTH_5_PERCENT_WIDGET));
	AbstractAdapter b6Adapter = new TextFieldAdapter(
		(JTextField) widgetRegistry
			.get(Constants.PRBL_BIRTH_6_PERCENT_WIDGET));
	AbstractAdapter b7Adapter = new TextFieldAdapter(
		(JTextField) widgetRegistry
			.get(Constants.PRBL_BIRTH_7_PERCENT_WIDGET));
	AbstractAdapter b8Adapter = new TextFieldAdapter(
		(JTextField) widgetRegistry
			.get(Constants.PRBL_BIRTH_8_PERCENT_WIDGET));

	AbstractAdapter preset1Adapter = new RadioButtonAdapter(
		(JRadioButton) widgetRegistry
			.get(Constants.PREDEFINED_1_WIDGET));
	AbstractAdapter preset2Adapter = new RadioButtonAdapter(
		(JRadioButton) widgetRegistry
			.get(Constants.PREDEFINED_2_WIDGET));
	AbstractAdapter preset3Adapter = new RadioButtonAdapter(
		(JRadioButton) widgetRegistry
			.get(Constants.PREDEFINED_3_WIDGET));
	AbstractAdapter preset4Adapter = new RadioButtonAdapter(
		(JRadioButton) widgetRegistry
			.get(Constants.PREDEFINED_4_WIDGET));
	AbstractAdapter preset5Adapter = new RadioButtonAdapter(
		(JRadioButton) widgetRegistry
			.get(Constants.PREDEFINED_5_WIDGET));
	AbstractAdapter preset6Adapter = new RadioButtonAdapter(
		(JRadioButton) widgetRegistry
			.get(Constants.PREDEFINED_6_WIDGET));
	AbstractAdapter preset7Adapter = new RadioButtonAdapter(
		(JRadioButton) widgetRegistry
			.get(Constants.PREDEFINED_7_WIDGET));
	AbstractAdapter preset8Adapter = new RadioButtonAdapter(
		(JRadioButton) widgetRegistry
			.get(Constants.PREDEFINED_8_WIDGET));
	AbstractAdapter preset9Adapter = new RadioButtonAdapter(
		(JRadioButton) widgetRegistry
			.get(Constants.PREDEFINED_9_WIDGET));
	AbstractAdapter preset10Adapter = new RadioButtonAdapter(
		(JRadioButton) widgetRegistry
			.get(Constants.PREDEFINED_10_WIDGET));
	AbstractAdapter preset11Adapter = new RadioButtonAdapter(
		(JRadioButton) widgetRegistry
			.get(Constants.PREDEFINED_11_WIDGET));
	AbstractAdapter preset12Adapter = new RadioButtonAdapter(
		(JRadioButton) widgetRegistry
			.get(Constants.PREDEFINED_12_WIDGET));

	ButtonAdapter rulesApplyAdapter = new ButtonAdapter(
		(JButton) widgetRegistry.get(Constants.RULES_APPLY_WIDGET));

	AbstractAdapter usePredefAdapter = new RadioButtonAdapter(
		(JRadioButton) widgetRegistry
			.get(Constants.USE_PREDEFINED_WIDGET));
	AbstractAdapter useUsrDefAdapter = new RadioButtonAdapter(
		(JRadioButton) widgetRegistry
			.get(Constants.USE_USER_DEFINED_WIDGET));
	AbstractAdapter userPrblAdapter = new RadioButtonAdapter(
		(JRadioButton) widgetRegistry
			.get(Constants.USE_PRBL_DEFINED_WIDGET));

	ArrayList<AbstractAdapter> simulationAdapterList = new ArrayList<AbstractAdapter>();
	simulationAdapterList.add(playAdapter);
	simulationAdapterList.add(stopAdapter);
	simulationAdapterList.add(pauseAdapter);
	simulationAdapterList.add(recordAdapter);
	simulationAdapterList.add(sliderAdapter);
	simulationAdapterList.add(spinnerAdapter);

	SimulationController simulationController = new SimulationController(
		model, simulationAdapterList);

	ArrayList<AbstractAdapter> rulesAdapterList = new ArrayList<AbstractAdapter>();

	rulesAdapterList.add(userBAdapter);
	rulesAdapterList.add(userSAdapter);
	rulesAdapterList.add(s0Adapter);
	rulesAdapterList.add(s1Adapter);
	rulesAdapterList.add(s2Adapter);
	rulesAdapterList.add(s3Adapter);
	rulesAdapterList.add(s4Adapter);
	rulesAdapterList.add(s5Adapter);
	rulesAdapterList.add(s6Adapter);
	rulesAdapterList.add(s7Adapter);
	rulesAdapterList.add(s8Adapter);
	rulesAdapterList.add(b0Adapter);
	rulesAdapterList.add(b1Adapter);
	rulesAdapterList.add(b2Adapter);
	rulesAdapterList.add(b3Adapter);
	rulesAdapterList.add(b4Adapter);
	rulesAdapterList.add(b5Adapter);
	rulesAdapterList.add(b6Adapter);
	rulesAdapterList.add(b7Adapter);
	rulesAdapterList.add(b8Adapter);
	rulesAdapterList.add(preset1Adapter);
	rulesAdapterList.add(preset2Adapter);
	rulesAdapterList.add(preset3Adapter);
	rulesAdapterList.add(preset4Adapter);
	rulesAdapterList.add(preset5Adapter);
	rulesAdapterList.add(preset6Adapter);
	rulesAdapterList.add(preset7Adapter);
	rulesAdapterList.add(preset8Adapter);
	rulesAdapterList.add(preset9Adapter);
	rulesAdapterList.add(preset10Adapter);
	rulesAdapterList.add(preset11Adapter);
	rulesAdapterList.add(preset12Adapter);

	rulesAdapterList.add(rulesApplyAdapter);

	rulesAdapterList.add(usePredefAdapter);
	rulesAdapterList.add(useUsrDefAdapter);
	rulesAdapterList.add(userPrblAdapter);

	RuleController ruleController = new RuleController(model,
		rulesAdapterList);

	AbstractAdapter boardHeightAdapter = new SpinnerAdapter(
		(JSpinner) widgetRegistry.get(Constants.BOARD_HEIGHT_WIDGET));
	AbstractAdapter boardWidthAdapter = new SpinnerAdapter(
		(JSpinner) widgetRegistry.get(Constants.BOARD_WIDTH_WIDGET));
	AbstractAdapter boardSizeApplyAdapter = new ButtonAdapter(
		(JButton) widgetRegistry.get(Constants.BOARD_SIZE_APPLY_WIDGET));

	ArrayList<AbstractAdapter> boardSizeAdapterList = new ArrayList<AbstractAdapter>();

	boardSizeAdapterList.add(boardSizeApplyAdapter);
	boardSizeAdapterList.add(boardWidthAdapter);
	boardSizeAdapterList.add(boardHeightAdapter);

	BoardSizeController boardSizeController = new BoardSizeController(
		model, boardSizeAdapterList);

	ArrayList<AbstractAdapter> generateSizeAdapterList = new ArrayList<AbstractAdapter>();

	AbstractAdapter generateLifeAdapter = new ButtonAdapter(
		(JButton) widgetRegistry
			.get(Constants.GENERATE_LIFE_APPLY_WIDGET));
	AbstractAdapter lifeSpinnerAdapter = new SpinnerAdapter(
		(JSpinner) widgetRegistry
			.get(Constants.GENERATE_LIFE_SPINNER_WIDGET));
	AbstractAdapter clearLifeAdapter = new ButtonAdapter(
		(JButton) widgetRegistry.get(Constants.ERASE_LIFE_WIDGET));

	generateSizeAdapterList.add(generateLifeAdapter);
	generateSizeAdapterList.add(lifeSpinnerAdapter);
	generateSizeAdapterList.add(clearLifeAdapter);

	AbstractAdapter generationAdapter = new LabelAdapter(
		(JLabel) widgetRegistry.get(Constants.ITERATION_WIDGET));
	AbstractAdapter currentRuleAdapter = new LabelAdapter(
		(JLabel) widgetRegistry.get(Constants.CURRENT_RULE_WIDGET));
	AbstractAdapter populationAdapter = new LabelAdapter(
		(JLabel) widgetRegistry.get(Constants.POPULATION_WIDGET));

	ArrayList<AbstractAdapter> statusAdapterList = new ArrayList<AbstractAdapter>();
	statusAdapterList.add(generationAdapter);
	statusAdapterList.add(currentRuleAdapter);
	statusAdapterList.add(clearLifeAdapter);
	statusAdapterList.add(populationAdapter);

	StatusController statusController = new StatusController(model,
		statusAdapterList);

	ArrayList<AbstractAdapter> loggerAdapterList = new ArrayList<AbstractAdapter>();
	loggerAdapterList.add(sliderAdapter);
	LoggerController loggerController = new LoggerController(model,
		loggerAdapterList);

	ArrayList<AbstractAdapter> statisticsAdapterList = new ArrayList<AbstractAdapter>();
	AbstractAdapter generateStatsAdapter = new ButtonAdapter(
		(JButton) widgetRegistry
			.get(Constants.GENERATE_STATISTICS_WIDGET));
	AbstractAdapter progressAdapter = new ProgressAdapter(
		(JProgressBar) widgetRegistry
			.get(Constants.GENERATE_PROGRESS_BAR));
	AbstractAdapter listAdapter = new ListAdapter((JList) widgetRegistry
		.get(Constants.STATISTICS_RESULTS_LIST));
	AbstractAdapter tabbedPaneAdapter = new VisibilityAdapter(
		(JComponent) widgetRegistry
			.get(Constants.GENERATE_TABBED_PANE_WIDGET));
	AbstractAdapter showGraphAdapter = new ButtonAdapter(
		(JButton) widgetRegistry.get(Constants.SHOW_GRAPH_WIDGET));
	AbstractAdapter graphOptionsAdapter = new ListAdapter(
		(JList) widgetRegistry.get(Constants.GRAPH_OPTIONS_WIDGET));

	AbstractAdapter canvasMouseAdapter = new MouseAdapter(
		(LifeCanvas) widgetRegistry.get(Constants.LIFE_CANVAS));
	AbstractAdapter canvasStatsAdapter = new LifeStatisticsDrawingAdapter(
		(LifeCanvas) widgetRegistry.get(Constants.LIFE_CANVAS));

	AbstractAdapter cellInfoAdapter = new ListAdapter(
		(JList) widgetRegistry.get(Constants.CELL_INFO_WIDGET));
	AbstractAdapter highlightClustersAdapter = new RadioButtonAdapter(
		(JToggleButton) widgetRegistry
			.get(Constants.HIGHLIGHT_CLUSTERS_WIDGET));

	statisticsAdapterList.add(generateStatsAdapter);
	statisticsAdapterList.add(progressAdapter);
	statisticsAdapterList.add(listAdapter);
	statisticsAdapterList.add(tabbedPaneAdapter);
	statisticsAdapterList.add(playAdapter);
	statisticsAdapterList.add(pauseAdapter);
	statisticsAdapterList.add(showGraphAdapter);
	statisticsAdapterList.add(graphOptionsAdapter);
	statisticsAdapterList.add(canvasMouseAdapter);
	statisticsAdapterList.add(cellInfoAdapter);
	statisticsAdapterList.add(canvasStatsAdapter);
	statisticsAdapterList.add(highlightClustersAdapter);
	statisticsAdapterList.add(sliderAdapter);

	StatisticsController statController = new StatisticsController(model,
		statisticsAdapterList);

	GenerationController generationController = new GenerationController(
		model, generateSizeAdapterList);

    }

}
